//
//  LXJHomeViewModel.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/29.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import YYModel

/// 首页的 viewModel
class LXJHomeViewModel: NSObject {
    
    /// 数据源数组懒加载
    var homeWeiboList: [LXJWeiboViewModel]?
    
    /// 请求首页数据
    func requestHomeWeiboInfo(success: ((Bool) -> ())?) -> Void {
        LXJNetworkManager.sharedManager.requestHomeWeiboInfo(access_token: (LXJOAuthViewModel.sharedOAuthViewModel.userAccount?.access_token)!, uid: (LXJOAuthViewModel.sharedOAuthViewModel.userAccount?.uid)!) { (response, error) in
            /// 数据请求失败
            if error != nil{
                print("微博首页网络数据请求失败,时报原因如下:\(error!)")
                /// 请求后的闭包回调
                success!(false)
                return
            }
            
            /// 执行到此请求成功
            guard let dic = response as? [String: Any] else{
                print("微博首页请求数据返回的数据不是标准数据")
                /// 请求后的闭包回调
                success!(false)
                return
            }
            
            ///  执行到此应是一个标准的字典,并完成字典转模型
            let weiboList = dic["statuses"] as! [[String: Any]]
            /// 微博模型的转换
            let weiboListArr = NSArray.yy_modelArray(with: LXJHomeWeiboModel.self, json: weiboList)
            
            /// 遍历weiboListArr,关联LXJWeiboViewModel
            var tempARR: [LXJWeiboViewModel] = [LXJWeiboViewModel]()
            for weiboModel in weiboListArr!{
                let weiboViewModel = LXJWeiboViewModel()
                weiboViewModel.homeWeiboList = weiboModel as? LXJHomeWeiboModel
                tempARR.append(weiboViewModel)
            }
            /// 属性关联
            self.homeWeiboList = tempARR
            
            /// 请求后的闭包回调
            success!(true)
        }
    }
}
